part of _;

extension on LoginPage {
  void controller_refreshCode() {
    codeParams = null;
    setState(() {});
    yRun(() => setState(() => codeParams = textController_account_loginname.text.isEmpty ? null : "?loginname=${textController_account_loginname.text}&t=${DateTime.now().microsecond}"));
  }

  void controller_login() {
    final loginname = textController_account_loginname.text;
    if (loginname.isEmpty) {
      yToast("请输入用户名");
      return;
    }
    final code = textController_account_code.text;
    if (code.isEmpty) {
      yToast("请输入验证码");
      return;
    }
    final password = textController_account_password.text;
    if (password.isEmpty) {
      yToast("请输入密码");
      return;
    }
    yPost<UserModel>(IFinal.Jkids.login_account, {
      "loginname": loginname,
      "password": EncodeUtils.encodePassword(password),
      "code": code,
    }, (isSuccess, results, code, message) {
      if (!isSuccess) {
        controller_refreshCode();
        return;
      }
      yToast("登录成功");
      AppUtils.saveUserModel(results!.first);
      AppUtils.goHome();
    }, onModel: (map) => UserModel.fromJson(map));
  }
}
